;-------------------------------------------------------------------------
;@
;@
;@̃\[XR[h́uAutoHotkeyvŋLqĂ܂B
;@
;@TCgFhttps://autohotkey.com/
;@WikiFhttp://ahkwiki.net/Top
;@
;@̃t@C͎ۂ̏Ɏg܂̂ŁA폜AړAsȂłB
;@
;@
;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@Jn@

;-------------------------------------------------------------------------

; XNvgݒ
#NoTrayIcon
#SingleInstance off
SetWorkingDir, %A_ScriptDir%

; ݒ
FormatTime,Time2,,yyyyMMddHHmmss
FileTypeVarNum = _1_2/_1_3/_2_2/_2_3/_3_2/_3_3/_4_2/_4_3/_5_2/_5_3/_6_2/_6_3/_7_2/_7_3/_8_2/_8_3
ProtocolVarNum = _1_2/_1_3/_2_2/_2_3
IniFile = ..\ݒ\setting.ini

; ݒtH_쐬
FileCreateDir, ..\ݒ

; ݒt@CEǂݍ
Loop, Parse, FileTypeVarNum, /
{
IniRead,  FileType%A_LoopField%,  %IniFile%,  Main,  FileType%A_LoopField%, __ERROR_%Time2%__
}

Loop, Parse, ProtocolVarNum, /
{
IniRead,  Protocol%A_LoopField%,  %IniFile%,  Main,  Protocol%A_LoopField%, __ERROR_%Time2%__
}

; ftHgl̑
if FileType_1_2 = __ERROR_%Time2%__
 FileType_1_2  = WEBy[W
if FileType_1_3 = __ERROR_%Time2%__
 FileType_1_3  = html htm

if FileType_2_2 = __ERROR_%Time2%__
 FileType_2_2  = 
if FileType_2_3 = __ERROR_%Time2%__
 FileType_2_3  = mp4 flv webm avi wmv mov qt vob ts

if FileType_3_2 = __ERROR_%Time2%__
 FileType_3_2  = 
if FileType_3_3 = __ERROR_%Time2%__
 FileType_3_3  = mp3 wav m4a ogg wma mid

if FileType_4_2 = __ERROR_%Time2%__
 FileType_4_2  = 摜
if FileType_4_3 = __ERROR_%Time2%__
 FileType_4_3  = jpg jpeg jpe gif png bmp

if FileType_5_2 = __ERROR_%Time2%__
 FileType_5_2  = eLXg
if FileType_5_3 = __ERROR_%Time2%__
 FileType_5_3  = txt ini inf log

if FileType_6_2 = __ERROR_%Time2%__
 FileType_6_2  = 
if FileType_6_3 = __ERROR_%Time2%__
 FileType_6_3  = 

if FileType_7_2 = __ERROR_%Time2%__
 FileType_7_2  = 
if FileType_7_3 = __ERROR_%Time2%__
 FileType_7_3  = 

if FileType_8_2 = __ERROR_%Time2%__
 FileType_8_2  = 
if FileType_8_3 = __ERROR_%Time2%__
 FileType_8_3  = 

if Protocol_1_2 = __ERROR_%Time2%__
 Protocol_1_2  = C^[lbg
if Protocol_1_3 = __ERROR_%Time2%__
 Protocol_1_3  = http https

if Protocol_2_2 = __ERROR_%Time2%__
 Protocol_2_2  = 
if Protocol_2_3 = __ERROR_%Time2%__
 Protocol_2_3  = 

; ǗҌ̊mF
TestFile = C:\TEMP-%Time2%t387segbjy57fy8y8ht59gs895r74t877yj58rsy67h8597w89h7t89%Time2%.log
FileAppend,, %TestFile%
IfNotExist, %TestFile%
{
Msgbox,,G[,ǗҌŎso܂łB𒆎~܂B
ExitApp
}
FileDelete, %TestFile%

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@EBhE\@

;-------------------------------------------------------------------------

; EBhEݒ肵ĕ\
Gui, Add, Text, x22 y29 w50 h20 +Right, \tgF
Gui, Add, Edit, x82 y29 w160 h20 Limit25 vSoftName, 
Gui, Add, Text, x252 y29 w380 h20 , iȗB25܂ŁBu _ . - v ȊO̔p͎gȂłB j
Gui, Add, Edit, x162 y59 w390 h20 vExeFile, 
Gui, Add, Button, x32 y59 w120 h20 , st@CI
Gui, Add, Text, x22 y119 w70 h20 +Right, t@C^CvF
Gui, Add, Text, x102 y119 w530 h20 +Left, `FbN邩Agq𒼐ړ͂ĂBi1pp16܂ŁBpXy[X؂Bj
Gui, Add, CheckBox, x42 y149 w20 h20 vFileType_1_1, 
Gui, Add, Edit, x62 y149 w90 h20 vFileType_1_2, %FileType_1_2%
Gui, Add, Edit, x162 y149 w490 h20 vFileType_1_3, %FileType_1_3%
Gui, Add, CheckBox, x42 y179 w20 h20 vFileType_2_1, 
Gui, Add, Edit, x62 y179 w90 h20 vFileType_2_2, %FileType_2_2%
Gui, Add, Edit, x162 y179 w490 h20 vFileType_2_3, %FileType_2_3%
Gui, Add, CheckBox, x42 y209 w20 h20 vFileType_3_1, 
Gui, Add, Edit, x62 y209 w90 h20 vFileType_3_2, %FileType_3_2%
Gui, Add, Edit, x162 y209 w490 h20 vFileType_3_3, %FileType_3_3%
Gui, Add, CheckBox, x42 y239 w20 h20 vFileType_4_1, 
Gui, Add, Edit, x62 y239 w90 h20 vFileType_4_2, %FileType_4_2%
Gui, Add, Edit, x162 y239 w490 h20 vFileType_4_3, %FileType_4_3%
Gui, Add, CheckBox, x42 y269 w20 h20 vFileType_5_1, 
Gui, Add, Edit, x62 y269 w90 h20 vFileType_5_2, %FileType_5_2%
Gui, Add, Edit, x162 y269 w490 h20 vFileType_5_3, %FileType_5_3%
Gui, Add, CheckBox, x42 y299 w20 h20 vFileType_6_1, 
Gui, Add, Edit, x62 y299 w90 h20 vFileType_6_2, %FileType_6_2%
Gui, Add, Edit, x162 y299 w490 h20 vFileType_6_3, %FileType_6_3%
Gui, Add, CheckBox, x42 y329 w20 h20 vFileType_7_1, 
Gui, Add, Edit, x62 y329 w90 h20 vFileType_7_2, %FileType_7_2%
Gui, Add, Edit, x162 y329 w490 h20 vFileType_7_3, %FileType_7_3%
Gui, Add, CheckBox, x42 y359 w20 h20 vFileType_8_1, 
Gui, Add, Edit, x62 y359 w90 h20 vFileType_8_2, %FileType_8_2%
Gui, Add, Edit, x162 y359 w490 h20 vFileType_8_3, %FileType_8_3%
Gui, Add, Text, x92 y389 w60 h20 +Right, ړ́F
Gui, Add, Edit, x162 y389 w490 h20 vFileType, 
Gui, Add, Button, x282 y589 w190 h40 , s
Gui, Add, Edit, x162 y549 w490 h20 vProtocol, 
Gui, Add, Text, x92 y429 w540 h20 , `FbN邩Aړ͂ĂBi1ppƔpu . - v20܂ŁBpXy[X؂Bj
Gui, Add, Text, x32 y429 w50 h20 , vgRF
Gui, Add, Text, x92 y549 w60 h20 +Right, ړ́F
Gui, Add, CheckBox, x42 y489 w20 h20 vProtocol_1_1, 
Gui, Add, Edit, x62 y489 w90 h20 vProtocol_1_2, %Protocol_1_2%
Gui, Add, Edit, x162 y489 w490 h20 vProtocol_1_3, %Protocol_1_3%
Gui, Add, CheckBox, x42 y519 w20 h20 vProtocol_2_1, 
Gui, Add, Edit, x62 y519 w90 h20 vProtocol_2_2, %Protocol_2_2%
Gui, Add, Edit, x162 y519 w490 h20 vProtocol_2_3, %Protocol_2_3%
Gui, Add, Text, x92 y459 w540 h20 , Ώۃ\tgΉ̃vgR͊֘AtȂłBJ[v댯܂B
Gui, Add, Button, x32 y89 w120 h20 , ACRt@CI
Gui, Add, Edit, x162 y89 w390 h20 vIconFile, 
Gui, Add, Text, x562 y59 w30 h20 , F
Gui, Add, Text, x562 y89 w30 h20 , F
Gui, Add, Edit, x602 y59 w50 h20 vExeFileArg, "`%1"
Gui, Add, Edit, x602 y89 w50 h20 vIconFileArg, `,0
Gui, Show, h653 w699, Win10-֘At⏕c[

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@ust@CIv{^ꍇ̏@

;-------------------------------------------------------------------------

Buttonst@CI:

;------------------------------------

; t@CI_CAO\
FileSelectFile, SelectedExeFile,,, Ώۃ\tg̃tH_JA\tg̎st@CIĂ, st@C (*.exe;*.com;*.bat;*.cmd;*.vbs;*.vbe;*.js;*.jse;*.wsf;*.wsh;*.msc)

; lۑ
Gui, Submit, NoHide

; Rg[ɒl
GuiControl,, ExeFile, %SelectedExeFile%

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@uACRt@CIv{^ꍇ̏@

;-------------------------------------------------------------------------

ButtonACRt@CI:

;------------------------------------

; t@CI_CAO\
FileSelectFile, SelectedIconFile,,, ACRi`.icojt@CAst@C̃ACR܂ރt@CIĂ, ACR܂ރt@C (*.ico;*.exe;*.dll)

; lۑ
Gui, Submit, NoHide

; Rg[ɒl
GuiControl,, IconFile, %SelectedIconFile%

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@usv{^ꍇ̏@

;-------------------------------------------------------------------------

Buttons:

;------------------------------------

; [U[͒l
Gui, Submit, NoHide

;------------------

; ϐuExeFilev̏

; u"v폜
StringReplace, ExeFile, ExeFile, ", , All

; 󂾂ꍇ
if ExeFile =
MsgBox,, G[, st@CIĂB
if ExeFile =
return

; ݂Ȃꍇ
IfNotExist, %ExeFile%
MsgBox,, G[, st@C݂܂B
IfNotExist, %ExeFile%
return

; tH_ꍇ
IfExist, %ExeFile%\
MsgBox,, G[, t@Cł͂܂B
IfExist, %ExeFile%\
return

;------------------

; ϐuIconFilev̏

; G[mFp̕ϐZbg
IconFileError = 0

; u"v폜
StringReplace, IconFile, IconFile, ", , All

; ACRt@Cw肳Ăꍇ
if IconFile <>
{

; Op^[̏ꍇu1v
IconFilePathError1 = %IconFile%
IconFilePathError1 := RegExReplace( IconFilePathError1, "^.*[*?<>|].*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
if IconFilePathError1 = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 MsgBox,, G[, ACRt@C̃pXɎgpoȂ܂܂Ă܂B
if IconFilePathError1 = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
IconFileError = 1
if IconFilePathError1 = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 return

; ݂Ȃꍇ
IfNotExist, %IconFile%
MsgBox,, G[, ACRt@C݂܂B
IfNotExist, %IconFile%
IconFileError = 1
IfNotExist, %IconFile%
return

; tH_ꍇ
IfExist, %IconFile%\
MsgBox,, G[, t@Cł͂܂B
IfExist, %IconFile%\
IconFileError = 1
IfExist, %IconFile%\
return

}

; G[ĂΒf
if IconFileError <> 0
return

;------------------

; ϐuSoftNamev̏

; \tg͂̏ꍇ
if SoftName =
{

; st@Cɂ
Loop, %ExeFile%
{
SoftName = %A_LoopFileName%
}

; ֎~Lu
SoftName := RegExReplace( SoftName, "[,:;/|!?+*=#$&@^'~""``\\(){}\[\]<>%]", "_" )

; 炷
SoftName := RegExReplace( SoftName, "^(.{11}).+(.{11})$", "$1___$2" )

; Op^[̏ꍇ
SoftNameError = %SoftName%
SoftNameError := RegExReplace( SoftNameError, "^.{26,}$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
SoftNameError := RegExReplace( SoftNameError, "^.*[,;:;/|!?+*=^'~%\\#$&@(){}\[\]<>``""].*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
if SoftNameError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 MsgBox,, G[, \tg̎ݒɎs܂B\tg͂ĂB
if SoftNameError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 return

; \tg͂Ăꍇ
}
else
{

; Op^[̏ꍇ
SoftNameError = %SoftName%
SoftNameError := RegExReplace( SoftNameError, "^.*[,;:;/|!?+*=^'~%\\#$&@(){}\[\]<>``""].*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
if SoftNameError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 MsgBox,, G[, \tg̋Lq܂B
if SoftNameError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
 return

}

; oĂȂꍇ
if SoftName =
MsgBox,, G[, \tg̃G[ɂso܂B
if SoftName =
return

;------------------

; ϐuFileTypev̏

; `FbNꂽt@C^Cv𑫂
if FileType_1_1 = 1
 FileType = %FileType% %FileType_1_3%
if FileType_2_1 = 1
 FileType = %FileType% %FileType_2_3%
if FileType_3_1 = 1
 FileType = %FileType% %FileType_3_3%
if FileType_4_1 = 1
 FileType = %FileType% %FileType_4_3%
if FileType_5_1 = 1
 FileType = %FileType% %FileType_5_3%
if FileType_6_1 = 1
 FileType = %FileType% %FileType_6_3%
if FileType_7_1 = 1
 FileType = %FileType% %FileType_7_3%
if FileType_8_1 = 1
 FileType = %FileType% %FileType_8_3%

; AXy[X𖳂
FileType := RegExReplace( FileType, "[\s]+", " " )
FileType := RegExReplace( FileType, "^[\s]+$", "" )

; Op^[ꍇ
FileTypeError = %FileType%
FileTypeError := RegExReplace( FileTypeError, "^.*[^a-zA-Z0-9 ].*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
FileTypeError := RegExReplace( FileTypeError, "^.*[a-zA-Z0-9]{17,}.*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
if FileTypeError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
MsgBox,, G[, t@C^Cv̋Lq܂B
if FileTypeError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
return

;------------------

; ϐuProtocolv̏

; `FbNꂽt@C^Cv𑫂
if Protocol_1_1 = 1
 Protocol = %Protocol% %Protocol_1_3%
if Protocol_2_1 = 1
 Protocol = %Protocol% %Protocol_2_3%

; AXy[X𖳂
Protocol := RegExReplace( Protocol, "[\s]+", " " )
Protocol := RegExReplace( Protocol, "^[\s]+$", "" )

; Op^[ꍇ
ProtocolError = %Protocol%
ProtocolError := RegExReplace( ProtocolError, "^.*[^a-zA-Z0-9 \.\-].*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
ProtocolError := RegExReplace( ProtocolError, "^.*[a-zA-Z0-9\.\-]{21,}.*$", "__Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__" )
if ProtocolError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
MsgBox,, G[, vgR̋Lq܂B
if ProtocolError = __Error_8hu4y37ty9875yt6ygfhtk9057h876h27g67f4h87g5rgth79832h78g__
return

;------------------

; t@C^CvvgR󂾂ꍇ
FileType_And_Protocol = %FileType%%Protocol%
if FileType_And_Protocol =
MsgBox,, G[, ut@C^CvvuvgRvŁA`FbN邩ړ͂ĂB
if FileType_And_Protocol =
return

;------------------

; WXg̏݃[v

; t@C^Cv
Loop, Parse, FileType, %A_Space%
{
LoopValue = %A_LoopField%
gosub, RegWrite_FileType
}

; vgR
Loop, Parse, Protocol, %A_Space%
{
LoopValue = %A_LoopField%
gosub, RegWrite_Protocol
}

;------------------

; bZ[W
Msgbox,, , ܂B`n`nʏ̊֘AtsĂB

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@WXg̏݁ut@C^Cvv@

;-------------------------------------------------------------------------

RegWrite_FileType:

;-------------------------------------

; uRegisteredApplicationsv
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\RegisteredApplications, FUASN-%SoftName%, SOFTWARE\FUASN-%SoftName%\Capabilities

; uFileAssociationsv
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\FUASN-%SoftName%\Capabilities\FileAssociations, .%LoopValue%, FUASN-%SoftName%-.%LoopValue%

; uDefaultIconv
if IconFile <> 
 RegWrite, REG_SZ, HKEY_CLASSES_ROOT, FUASN-%SoftName%-.%LoopValue%\DefaultIcon, , %IconFile%%IconFileArg%

; ucommandv
RegWrite, REG_SZ, HKEY_CLASSES_ROOT, FUASN-%SoftName%-.%LoopValue%\shell\Open\command, , "%ExeFile%" %ExeFileArg%

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@WXg̏݁uvgRv@

;-------------------------------------------------------------------------

RegWrite_Protocol:

;-------------------------------------

; uRegisteredApplicationsv
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\RegisteredApplications, FUASN-%SoftName%, SOFTWARE\FUASN-%SoftName%\Capabilities

; uURLAssociationsv
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\FUASN-%SoftName%\Capabilities\URLAssociations, %LoopValue%, FUASN-%SoftName%-%LoopValue%

; ucommandv
RegWrite, REG_SZ, HKEY_CLASSES_ROOT, FUASN-%SoftName%-%LoopValue%\shell\Open\command, , "%ExeFile%" "`%1"

; uURL Protocolv
RegWrite, REG_SZ, HKEY_CLASSES_ROOT, %LoopValue%, URL Protocol,

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@ݒt@CEo@

;-------------------------------------------------------------------------

OutputSetting:

;-------------------------------------

; ݒt@C̃tbV
FileDelete, %IniFile%
FileAppend, [Main]`n, %IniFile%

; ut@C^Cvv̏o
Loop, Parse, FileTypeVarNum, /
{
CurrentFileTypeVarName = FileType%A_LoopField%
CurrentFileTypeVar := %CurrentFileTypeVarName%
if CurrentFileTypeVar = __ERROR_%Time2%__
 CurrentFileTypeVar =
FileAppend, FileType%A_LoopField%=%CurrentFileTypeVar%`n, %IniFile%
}

; uvgRv̏o
Loop, Parse, ProtocolVarNum, /
{
CurrentProtocolVarName = Protocol%A_LoopField%
CurrentProtocolVar := %CurrentProtocolVarName%
if CurrentProtocolVar = __ERROR_%Time2%__
 CurrentProtocolVar =
FileAppend, Protocol%A_LoopField%=%CurrentProtocolVar%`n, %IniFile%
}

;------------------------------------

; ̏̏I
return

;-------------------------------------------------------------------------







;-------------------------------------------------------------------------

;@{^ꍇ̏@

;-------------------------------------------------------------------------

GuiClose:

;-------------------------------------

; [U[͒l
Gui, Submit, NoHide

; ݒ菑o
GoSub, OutputSetting

;-------------------------------------

; \tgI
ExitApp

;-------------------------------------------------------------------------
